package com.android.dfsc;



import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public abstract class CommonProcess  {

	protected static final int PROCESS_DIALOG = 0;
	protected static final int ALERT_SUCCESSFUL_DIALOG = 1;
	protected static final int ALERT_FAIL_DIALOG = 2;
	
	protected int onoff = 0;
	private int successfulFlag = 0;
	protected int activeItem = 0;
	protected Activity context;
		
	public CommonProcess(Activity context) {
		this.context = context;
	}
	
	abstract public void setLayout();
	
	abstract protected List<Map<String, Object>> getData();
		
	protected final BroadcastReceiver processCmdReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context lcontext, Intent intent) {

			context.unregisterReceiver(this);
			context.dismissDialog(PROCESS_DIALOG);

			if ((Integer) (intent.getExtras().get("result")) == successfulFlag ) {
				context.showDialog(ALERT_FAIL_DIALOG);
			} else
				context.showDialog(ALERT_SUCCESSFUL_DIALOG);

		}
	};
	
	public int getSuccessfulFlag() {
		return successfulFlag;
	}

	public void setSuccessfulFlag(int successfulFlag) {
		this.successfulFlag = successfulFlag;
	}
	
	protected void setViews(){
		View settingView =  context
				.findViewById(R.id.settingLayout);
		View relayView =  context
				.findViewById(R.id.relayLayout);
		 
		View warmView =  context
				.findViewById(R.id.warmLayout);
		
		LinearLayout mainLayout= (LinearLayout) context
				.findViewById(R.id.mainLinearLayout);
		
		if (settingView  != null)
			mainLayout.removeView(settingView);
		
		if (relayView  != null)
			mainLayout.removeView(relayView);
		
		if (warmView  != null)
			mainLayout.removeView(warmView);
		
		RelativeLayout mRelativeLayout = (RelativeLayout) context
				.findViewById(R.id.relativeLayout2);
		mRelativeLayout.setVisibility(View.VISIBLE);
		//mainLayout.removeView(mRelativeLayout);
		
		mRelativeLayout = (RelativeLayout) context
				.findViewById(R.id.relativeLayout3);
		mRelativeLayout.setVisibility(View.VISIBLE);
		//mainLayout.removeView(mRelativeLayout);
	}
	
	public class MyListAdapter extends SimpleAdapter {

		public MyListAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
			// TODO Auto-generated constructor stub
		}

		// @Override
		public View getView(int position, View convertView, ViewGroup parent) {

			convertView = super.getView(position, convertView, parent);

			if (position == activeItem) {
				convertView.setBackgroundColor(Color.parseColor("#ADADAD"));
				((TextView) convertView.findViewById(R.id.contentName))
						.setTextColor(Color.parseColor("#FFFFFF"));
				((TextView) convertView.findViewById(R.id.contentDesc))
						.setTextColor(Color.parseColor("#FFFFFF"));
			}

			else
				convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));

			return convertView;
		}
	}
}
